<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\index\model;

use think\Model;

class ProxyWithdraw extends Model{

    protected $autoWriteTimestamp = true;

    /**
     * @param array $where
     * @param int $pageSize
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where= [], $pageSize=10,$field='*'){
        $list = $this
            ->field($field)
            ->order('create_time desc')
            ->where($where)
            ->paginate($pageSize);
        return $list;
    }

    /**
     * 格式时间
     * @param $value
     * @return false|string
     */
    public function getUpdateTimeAttr($value){
        return  $value?date('Y-m-d H:i:s',$value):'--';
    }

    /**
     *
     * @param $name
     * @param $money
     * @param int $proxy_id
     * @param string $out
     * @return $this|bool
     */
    public function addNew($name='',$money=0,$proxy_id=0,&$out=''){
        $valid = new \app\admin\validate\ProxyWithdraw();
        $data = [
            'name'=>$name,
            'proxy_id'=>$proxy_id,
            'money'=>$money,
            'status'=>1,
            'update_time'=>0,
            'order_id'=>build_order_no()
        ];
        if(!$valid->check($data)){
            $out = $valid->getError();
            return false;
        }
        $this->data($data);
        $out = $data;
        return $this;
    }

    /**
     * 获取数据
     * @param int $id
     * @param string $field
     * @param int $limit
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getListByProxyId($id=0,$field='*',$limit=10){
        $list = $this
            ->field($field)
            ->where('proxy_id','eq',$id)
            ->paginate($limit);
        return $list;
    }

    /**
     * 检测申请
     * @param int $id
     * @param string $out
     * @return bool
     */
    public function check($id=0,&$out=''){
        $start = date('Y-m-d',time());
        $end = date('Y-m-d',time());
        $count1 = $this
            ->where('proxy_id','eq',$id)
            ->where('status','eq',1)
            ->count();
        if($count1){
            $out = '您有一个提现待处理，请耐心等待处理';
            return false;
        }
        $count = $this
            ->whereBetweenTime('create_time',"{$start} 00:00:00","{$end} 23:59:59")
            ->where('proxy_id','eq',$id)
            ->whereIn('status','2,3,4')
            ->count();
        if($count){
            $out = '一天内只能提现一次,请明日再来';
            return false;
        }
        return true;
    }
}